#include "mdichild.h"

#include <QFile>
#include <QFileDialog>
#include <QFileInfo>
#include <QMessageBox>
#include <QTextStream>
#include <QCloseEvent>
#include <QAbstractButton>
#include <QMenu>

MdiChild::MdiChild(QWidget *parent) :
    QTextEdit(parent)
{
    setAttribute(Qt::WA_DeleteOnClose);
    isUntitled=true;
}

void MdiChild::newFile()
{
    static int sequenceNum=0;
    //isUntitled
    currFile=QString("Untitled%1.txt").arg(++sequenceNum);
    setWindowTitle(currFile+"[*]"+tr("MultiFileEdit"));
    connect(document(),&QTextDocument::contentsChanged,this,&MdiChild::documentWasModifitied);
}

bool MdiChild::loadFile(const QString &filename)
{
    QFile file (filename);
    if(!file.open((QIODevice::ReadOnly|QFile::Text))){
        QMessageBox::warning(this,tr("MultiTextEdit"),tr("Unable to open %1").arg(currFile));
        return false;
    }
    QTextStream in(&file);
//    QApplication::setOverrideCursor(Qt::WaitCursor)
    setPlainText(in.readAll());

    setCurrentFile(filename);
    connect(document(),&QTextDocument::contentsChanged,this,&MdiChild::documentWasModifitied);
    return true;

}

bool MdiChild::save()
{
    if(isUntitled){
        return saveAs();
    }else{
        return saveFile(currentFile());
    }
}

bool MdiChild::saveAs()
{
    QString fileName=QFileDialog::getSaveFileName(this,tr("SaveAs"),currFile);
    if(fileName.isEmpty()){
        return false;
    }else{
        return saveFile(fileName);
    }
}

bool MdiChild::saveFile(const QString &filename)
{
    QFile file(filename);
    if(!file.open((QIODevice::WriteOnly|QFile::Text))){
        QMessageBox::warning(this,tr("saveFile"),tr("SaveFile %1 Failed").arg(filename));
        return false;
    }
    QTextStream out(&file);
    out<<toPlainText();
    setCurrentFile(filename);
    return true;
}

QString MdiChild::userFriendlyCurrentFile()
{
    return QFileInfo(currentFile()).fileName();
}

void MdiChild::closeEvent(QCloseEvent *event)
{
    if(maybeSave()){
        event->accept();
    }else{
        event->ignore();
    }
}

void MdiChild::documentWasModifitied()
{
    setWindowModified(document()->isModified());
}

bool MdiChild::maybeSave()
{
   if(document()->isModified()) {
       QMessageBox box;
       box.setWindowTitle(tr("MultiWinTextEdit"));
       box.setText(tr("Save %1 ?").arg(userFriendlyCurrentFile()));
       box.setIcon(QMessageBox::Warning);
       QPushButton *yesBtn=box.addButton(tr("Yes"),QMessageBox::YesRole);
       box.addButton(tr("No"),QMessageBox::NoRole);
       QPushButton *cancelBtn=box.addButton(tr("Cancel"),QMessageBox::RejectRole);
       box.exec();
       if(box.clickedButton()==(QAbstractButton *)yesBtn){
           return save();
       }else if(box.clickedButton()==(QAbstractButton *)cancelBtn){
           return false;
       }
   }
   return true;
}

void MdiChild::setCurrentFile(const QString &fileName)
{
    currFile=QFileInfo(fileName).canonicalFilePath();
    isUntitled=false;
    document()->setModified(false);
    setWindowModified(false);
    setWindowTitle(userFriendlyCurrentFile()+"[*]");
}


void MdiChild::contextMenuEvent(QContextMenuEvent *event)
{
	QMenu *menu=new QMenu(this);
	QAction *undo=menu->addAction(tr("撤销"),this,
								  &QTextEdit::undo,QKeySequence::Undo);
	undo->setEnabled(document()->isUndoAvailable());

	QAction *redo=menu->addAction(tr("恢复"),this,
								  &QTextEdit::redo,QKeySequence::Redo);
	redo->setEnabled(document()->isRedoAvailable());
	menu->addSeparator();
	QAction *cut=menu->addAction(tr("剪切"),this,
								 &QTextEdit::cut,QKeySequence::Cut);
	cut->setEnabled(textCursor().hasSelection());

	QAction *copy=menu->addAction(tr("复制"),this,
								 &QTextEdit::copy,QKeySequence::Copy);
	copy->setEnabled(textCursor().hasSelection());

	menu->addAction(tr("粘贴"),this,&QTextEdit::paste,QKeySequence::Paste);
	QAction *clear=menu->addAction(tr("清空"),this,
								 &QTextEdit::clear);
	clear->setEnabled(!document()->isEmpty());
	menu->addSeparator();
	QAction *selectall=menu->addAction(tr("全选"),this,&QTextEdit::selectAll,QKeySequence::SelectAll);
	selectall->setEnabled(!document()->isEmpty());
	menu->exec(event->globalPos());


}
